/*---------------------------------------------------------------------------
 * �2005 NORSYS
 * main author : nono
 *
 * This software is a computer program whose purpose is to provide abstraction
 * for accessing directory data sources within java applications.
 *
 * This software is governed by the CeCILL  license under French law and
 * abiding by the rules of distribution of free software.  You can  use, 
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info". 
 *
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability. 
 *
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or 
 * data to be ensured and,  more generally, to use and operate it in the 
 * same conditions as regards security. 
 *
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
 *
 * Created on Nov 2, 2005
 * --------------------------------------------------------------------------*/

package speculoos.jndi.mappers;

import java.util.Map;

import javax.naming.NamingException;
import javax.naming.directory.ModificationItem;

import speculoos.core.JoinMapper;
import speculoos.core.Mapper;
import speculoos.core.MapperException;
import speculoos.jndi.ModifyMapper;
import speculoos.jndi.mappers.ModificationsVariables.AttributeAndOperation;
import speculoos.utils.VariableString;

/**
 * Implementation of modify LDAP operation.
 * 
 * @author nono
 * @version $Id$
 */
public class ModifyMapperImpl extends JNDIMapper implements ModifyMapper {

	private Mapper transform = null;

	private ModificationsVariables operations = null;

	/**
	 * Create modify mapper with given name.
	 * 
	 * @param value
	 *            name of the mapper. May not be null or empty.
	 */
	public ModifyMapperImpl(String value) {
		super(value);
	}

	/*
	 * taxes as input an array of modification objects and apply them on root.
	 * 
	 * @see fr.norsys.mapper.jndi.ModifyMapper#modify(java.util.Map,
	 *      java.util.Map)
	 */
	public Object modify(Object input, Map env) throws MapperException {
		return map(input, env);
	}

	protected Object doMap(Object input, Map env) throws MapperException {
		/* do query */
		try {
			/* create modifications */
			ModificationItem[] mods = null;
			if (transform != null)
				mods = (ModificationItem[]) transform.map(input, env);
			else if (operations != null) {
				/* defined operations */
				mods = (ModificationItem[]) operations.map(null, env);
			} else
				throw new MapperException(getName()
						+ ": No transformation defined");
			if (log.isDebugEnabled())
				log.debug("[" + getName() + "] modifiying " + _root + " with "
						+ input);
			directory.modifyAttributes(_root, mods);
			if (log.isDebugEnabled())
				log.debug("[" + getName() + "] done");
		} catch (NamingException ne) {
			throw new speculoos.core.MapperException(
					"Error in modificaiton operation", ne);
		} catch (ClassCastException e) {
			throw new MapperException(getName() + ": invalid input type "
					+ input.getClass() + ", expected ModificationItem[]");
		}
		return input;
	}

	/**
	 * Sets the transformation map for this modifier.
	 * 
	 * @param class1
	 * @param map
	 */
	public void setOutput(Class class1, Map map) {
		this.transform = new JoinMapper(
				AttributesFromInput.create(class1, map),
				new ModificationsFromAttributes());
	}

	/**
	 * Defines the operations set to use.
	 * 
	 * @param variables
	 */
	public void setOperations(ModificationsVariables variables) {
		this.operations = variables;
	}

	/**
	 * Adds a new operation to this mapper. This method creates the opeartion
	 * set if it does not exist.
	 * 
	 * @param ao
	 * @param string
	 */
	public void operation(AttributeAndOperation ao, VariableString string) {
		if (this.operations == null)
			throw new IllegalArgumentException(
					getName()
							+ ": No modifications set defined. This means that mapper is badly configured.");
		this.operations.operation(ao, string);
	}

}
